
local meta = FindMetaTable( "Player" )
if (!meta) then return end 

function meta:SetViewPosition(vec) //todo: make this work
	umsg.Start("SetViewPos", self)
	if vec then
		umsg.Vector(vec)
	end
	umsg.End() 
end

function meta:SetViewEnt(ent)
	self:SpectateEntity(ent)
	umsg.Start("SetViewEnt", self)
	if ent then
		umsg.Entity(ent)
	end
	umsg.End()
end

function meta:CreateViewCamera( vec, ent ) //you only should call this once when the player initially spawns!
	
	local camera = ents.Create("view_camera")
	camera:SetPos(self:GetPos())
	camera:Spawn()
	camera:SetOffset(vec)
	if ent then
		camera:SetPlayer(ent)
	else
		camera:SetPlayer(self)
	end

	if self:GetNetworkedEntity( "Camera", NULL ) != NULL then
		self:GetNetworkedEntity( "Camera" ):Remove()
	end
	
	self:SetNetworkedEntity( "Camera", camera )
	self:SetViewEntity( camera )

end

function meta:MakeRagdoll(eyeview)
	self:CreateRagdoll()
	if eyeview then
		umsg.Start("SetViewClientCorpse", self)
		umsg.Bool(true)
		umsg.End()
	end
end

function meta:SetupPlayerTable(override)
	local playermodel = self:GetInfo( "cl_playermodel" )
	local modelname = string.lower(player_manager.TranslatePlayerModel( playermodel ))
	if override then
		modelname = override
	end
	self.Info = nil
	for k,v in pairs(PlayerTables) do
		if string.find(modelname,v.Model) then
			self.Info = v
		end
	end
	if self.Info == nil then
		self.Info = PlayerTables[26]
	end
	self:SetModel(modelname)
end

function meta:GetPlayerTable()
return self.Info end

function meta:ClientSound(sound)
	umsg.Start("PlaySound", self)
	umsg.String(sound)
	umsg.End() 
end

function meta:VoiceSound(sound,vol,delay)
	if not self:Alive() then return end
	vol = vol or 100
	self.Pitch = self.Pitch or {95,105}
	if self.SoundDelay then
		if self.SoundDelay < CurTime() then
			if delay != nil then
				timer.Simple(delay, function(self,sound,vol) 
				if self.SoundDelay < CurTime() then
					if self:IsValid() then
						self:EmitSound(sound,vol,100) end
						self.SoundDelay = CurTime() + 2
					end
				end, self, sound, vol)
			else
				self.SoundDelay = CurTime() + 2
				self:EmitSound(sound,vol,100)
			end
		end
	else
		self.SoundDelay = CurTime() + 2
		if delay != nil then
			timer.Simple(delay, function(self,sound,vol) 
			if self.SoundDelay < CurTime() then
				if self:IsValid() then
					self:EmitSound(sound,vol,100) end
					self.SoundDelay = CurTime() + 2
				end
			end, self, sound, vol)
		else
			self.SoundDelay = CurTime() + 2
			self:EmitSound(sound,vol,100)
		end
	end
end

function meta:SetSpeed(num)
	self.WalkSpeed = num
end

function meta:SetMaxHealth(value,apply)
	self.MaxHealth = value
	umsg.Start("MaxHealth", self)
	umsg.Short(value)
	umsg.End() 
	if apply then
		self:SetHealth(value)
	end
end

function meta:AddHealth(value)
	if self:Health() == self.MaxHealth or not self:Alive() then return end
	self:SetHealth(math.Clamp(self:Health() + value, 0, self.MaxHealth))
	if value > 10 then
		umsg.Start("HealthKit", self)
		umsg.End() 
	else
		umsg.Start("HealthTiny", self)
		umsg.End() 
	end
end

function meta:TakeHealth(value)
	if self:Health() - value <= 0 then
		self:Kill()
	else
		self:SetHealth(math.Clamp(self:Health() - value, 0, self.MaxHealth))
	end
end
